package com.atguigu.test.config;

import com.atguigu.test.mvc.properties.MyThreadProperties;
import com.atguigu.test.po.Man;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zp
 * @version 1.0
 * @description: springboot自动装配配置类
 * @date 2023/2/6 18:55
 */
@Configuration
@EnableConfigurationProperties({MyThreadProperties.class}) // 注入一个属性配置类 绑定属性 可以和配置文件结合使用
@Import(MyConfigurationImportSelector.class) // 导入自动扫描类 扫描spring.factories文件当中的配置类 完成优先注入
public class SpringBootConfig {
    @Bean
    public ServletWebServerFactory servletWebServerFactory() {
        return new TomcatServletWebServerFactory();
    }

    @Bean
    public ThreadPoolExecutor poolExecutor(MyThreadProperties threadProperties) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(threadProperties.getCorePoolSize(),
                threadProperties.getMaximumPoolSize(),
                threadProperties.getKeepAliveTime(),
                TimeUnit.SECONDS
                , new ArrayBlockingQueue<>(100));
        return threadPoolExecutor;
    }

    /**
     * @description:  使用条件注解 ConditionalOnMissingBean 当不存在Man类 才注入
     * @author zp
     * @date: 2023/2/6 19:52
     * @return Bean1
     */
    @ConditionalOnMissingBean(Man.class)
    @Bean
    public Bean1 bean1(){
        return new Bean1();
    }

    /**
     * @description:  使用条件注解 ConditionalOnBean 当存在Man类 才注入
     * @author zp
     * @date: 2023/2/6 19:52
     * @return Bean2
     */
    @ConditionalOnBean(Man.class)
    @Bean
    public Bean2 bean2(){
        return new Bean2();
    }

    static class Bean1{

    }

    static class Bean2{

    }
}
